package com.woniuxy.service.impl;

import com.woniuxy.dao.*;
import com.woniuxy.entity.*;
import com.woniuxy.service.InpatientDrugService;
import com.woniuxy.service.InpatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InpatientServiceImpl implements InpatientService {
    @Autowired
    InpatientDao inpatientDao;
    @Autowired
    InpatientDrugDao inpatientDrugDao;
    @Autowired
    InpatientInspectDao inpatientInspectDao;
    @Autowired
    InpatientOperationDao inpatientOperationDao;
    @Autowired
    InpatientOrderDao inpatientOrderDao;
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    PrescriptionDao prescriptionDao;
    @Autowired
    DrugDao drugDao;

    @Override
    public PageBean<Patient> getReadyInPatient(int pageIndex, int pageSize) {
        Prescription prescription = new Prescription();
        prescription.setPrescriptionStatus(2);
        List<Prescription> prescriptions = prescriptionDao.selectPrescriptionByCondition(prescription, pageIndex, pageSize);
        List<Patient> patients = new ArrayList<>();
        for (Prescription prescription1 : prescriptions) {
            patients.add(prescription1.getPatient());
        }
        int totalRecord = prescriptionDao.getPrescriptionNum(prescription);
        PageBean pageBean = new PageBean(patients, pageIndex, pageSize, totalRecord);
        return pageBean;
    }

    @Override
    public void addInpatient(Inpatient inpatient) {
        Prescription prescription = prescriptionDao.getPrescriptByPatientId(inpatient.getInpatientId());
        int doctorId = prescription.getDoctorId();
        Doctor doctor = doctorDao.selectDoctorById(doctorId);
        inpatient.setDoctor(doctor);
        inpatient.setDoctorId(doctorId);
        inpatient.setInpatientIllness(prescription.getConditionDetails());
        inpatient.setInpatientTime(new Date());
        inpatientDao.insertInpatient(inpatient);
        prescriptionDao.updateStatusByPatientId(inpatient.getInpatientId());
    }

    @Override
    public PageBean<Inpatient> getInpatients(Inpatient inpatient, int pageIndex, int pageSize) {
        PageBean pageBean = new PageBean();
        List<Inpatient> inpatients = inpatientDao.selectInpatients(inpatient, pageIndex, pageSize);
        for (Inpatient inpatient1 : inpatients) {
            inpatient1.setDoctor(doctorDao.selectDoctorById(inpatient1.getDoctorId()));
        }
        pageBean.setBeanList(inpatients);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(inpatientDao.selectInpatientsCount(inpatient));
        return pageBean;
    }

    @Override
    public void deleteInpatient(int inpatientId) {
        inpatientDao.deleteInpatient(inpatientId);
    }

    @Override
    public InpatientOrder insertOrder(int inpatientId) {
        InpatientOrder inpatientOrder = new InpatientOrder();
        List<InpatientDrug> drugs = inpatientDrugDao.selectInpatientDrug(inpatientId);
        List<InpatientInspect> inspects = inpatientInspectDao.selectInpatientInspect(inpatientId);
        List<InpatientOperation> operations = inpatientOperationDao.selectInpatientOperation(inpatientId);
        BigDecimal drugPrice = new BigDecimal(0);
        if (!drugs.isEmpty()) {
            for (InpatientDrug drug : drugs) {
                drugPrice = drugPrice.add(drug.getInpatientDrugPrice());
            }
        }
        BigDecimal inspectPrice = new BigDecimal(0);
        if (!inspects.isEmpty()) {
            for (InpatientInspect inspect : inspects) {
                inspectPrice = inspectPrice.add(inspect.getInpatientInspectPrice());
            }
        }
        BigDecimal operationPrice = new BigDecimal(0);
        if (!operations.isEmpty()) {
            for (InpatientOperation operation : operations) {
                operationPrice = operationPrice.add(operation.getInpatientOperationPrice());
            }
        }
        inpatientOrder.setDrugPrice(drugPrice);
        System.out.println(drugPrice);
        inpatientOrder.setInspectPrice(inspectPrice);
        inpatientOrder.setOperationPrice(operationPrice);
        inpatientOrder.setInpatientTotalPrice(drugPrice.add(inspectPrice).add(operationPrice));
        inpatientOrder.setClosingDate(new Date());
        inpatientOrder.setInpatientId(inpatientId);
        inpatientOrderDao.insertOrder(inpatientOrder);
        return inpatientOrder;
    }

    @Override
    public void updateOrderState(int inpatientOrderId) {
        inpatientOrderDao.updateOrderState(inpatientOrderId);
    }
}
